package me.keiwu.admin.service.impl;


import me.keiwu.admin.domain.user.Label;
import me.keiwu.admin.domain.user.UserLabel;
import me.keiwu.admin.dao.LabelRepository;
import me.keiwu.admin.service.LabelService;
import me.keiwu.admin.service.UserLabelService;
import me.keiwu.admin.util.EntityUtil;
import me.keiwu.admin.vo.LabelSearch;
import me.keiwu.admin.vo.UserLabelSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;


/**
 * Created by kei on 07/08/2017.
 */
@Service
@Transactional
public class LabelServiceImpl implements LabelService {


    @Autowired
    private LabelRepository labelRepository;
    @Autowired
    private UserLabelService userLabelService;



    @Override
    public Long add(Label label) {

        Optional<Label> res = labelRepository.findByLabelName(label.getLabelName());
        if (res.isPresent()){
            return res.get().getId();
        }

        label.setCreatedBy("admin");
        label.setCreatedDate(new Date());

        Label dbLabel = labelRepository.save(label);


        return dbLabel.getId();
    }

    @Override
    public boolean delete(Long id) {
        labelRepository.delete(id);
        //删除用户标签信息
        userLabelService.deleteByLabelId(id);

        return true;
    }

    @Override
    public boolean update(Label label) {
        Label dbLabel = labelRepository.findOne(label.getId());
        if (dbLabel == null) return false;
        dbLabel.setLastModifiedBy("admin");
        dbLabel.setLastModifiedDate(new Date());

//        EntityUtil.setModifiedBy(dbLabel);
        dbLabel = (Label)dbLabel.merge(label);
        labelRepository.save(dbLabel);
        return true;
    }

    @Override
    public Label detail(Long id) {
        return labelRepository.findOne(id);

    }

    @Override
    public Page<Label> list(LabelSearch labelSearch) {
        Specification<Label> spec;
        spec = (root, query, cb) -> {

            List<Predicate> predicateList = new ArrayList<Predicate>();
            if(labelSearch.getId() != null){
                Expression<String> id = root.get("id");
                predicateList.add(cb.equal(id, labelSearch.getId()));
            }
            if(labelSearch.getLabelName() != null){
                Expression<String> username = root.get("label_name");
                predicateList.add(cb.equal(username, labelSearch.getLabelName()));
            }

            Predicate[] arrayPredicates = new Predicate[predicateList.size()];

            return cb.and(predicateList.toArray(arrayPredicates));
        };

        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(labelSearch.getPage(), labelSearch.getSize(), sort);

        Page<Label> pageResult= labelRepository.findAll(spec, pageable);
        List<Label> list = pageResult.getContent();
        forMat(list);
        return  pageResult;
    }

    //设置用户信息
    private List<Label> forMat(List<Label> list){
        UserLabelSearch userLabelSearch = new UserLabelSearch();
        userLabelSearch.setPage(0);
        userLabelSearch.setSize(20);
        for(Label label: list){
            userLabelSearch.setLabelId(label.getId());
            Page<UserLabel> page =userLabelService.findUser(userLabelSearch);
            label.setUserLabelPage(page);
        }
        return list;
    }
}
